<template>
  <div id="main">
    <div class="item-box">
    <el-skeleton style="width:  100%" :loading="loading" animated>
      <template #template>
        <div style="margin-bottom:30px;border:1px solid #eee;padding:5px;display:flex;justify-content: space-between;margin: 30px 0px;">
          <!-- 左侧 -->
          <div style="width:60%;">
            <el-skeleton-item variant="h2"  style="width:75%"/>
            <el-skeleton-item variant="text" style="width:55%;margin:40px 0;" />
            <el-skeleton-item variant="text"  style="width:50%"/>
          </div>
          <!-- 右侧 -->
          <div style="width:35%;height:150px;">
            <el-skeleton-item
              variant="image"
              style="border-radius: 4px;width: 100%; height: 100%;"
            />
          </div>
        </div>
        <div style="margin-bottom:30px;border:1px solid #eee;padding:5px;display:flex;justify-content: space-between;margin: 30px 0px;">
          <!-- 左侧 -->
          <div style="width:60%;">
            <el-skeleton-item variant="h2"  style="width:75%"/>
            <el-skeleton-item variant="text" style="width:55%;margin:40px 0;" />
            <el-skeleton-item variant="text"  style="width:50%"/>
          </div>
          <!-- 右侧 -->
          <div style="width:35%;height:150px;">
            <el-skeleton-item
              variant="image"
              style="border-radius: 4px;width: 100%; height: 100%;"
            />
          </div>
        </div>
      </template>
      <template #default>
        <el-card
          shadow="hover"
          @click="$router.push(`/articleDetail/${item.id}`)"
          class="item"
          v-for="(item, index) in cuurentListBlog"
          :key="index"
        >
          <el-row>
            <el-col :span="14" class="item-left">
              <h2>{{ item.title }}</h2>
              <p class="text">{{ item.minorContent }}</p>
              <p class="span">
                <span v-text="getDateYear(item.date)"> </span>
                <span>前发布 | </span>
                <span>{{ item.commentNum }} 条评论</span>
              </p>
            </el-col>

            <el-col :span="8" :offset="2" class="item-right">
              <img :src="BASEURL + item.img" alt="" />
            </el-col>
          </el-row>
        </el-card>
         <el-card id="no-blog" v-if="allSize == 0">
          暂无博客文章数据
        </el-card>
      </template>
    </el-skeleton>
      <!-- 底部 -->
      <el-row class="footer" v-if="allSize > 5">
        <el-pagination
          background
          page-size="5"
          layout="prev, pager, next"
          :total="allSize"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </el-row>
    </div>
  </div>
</template>

<script>
import { computed, defineComponent, ref } from "vue";
import useGetMonth from "../hooks/userGetMonth";

import { getPageBlog, BASEURL } from "../api/index";
import { ElMessage } from "element-plus";
export default defineComponent({
  name: "HomeRight",
  setup() {

    let loading = ref(true)

    //计算距离发布的时间差
    const getDateYear = computed(() => {
      return function (dataStr) {
        return useGetMonth(dataStr);
      };
    });
    //当前页博客列表
    let cuurentListBlog = ref([]);

    //总的博客数量
    let allSize = ref(1);
    // 当前页的数码
    let indexPage = ref(1);
    //获取文章列表,默认先展示最新前五条博客
    getPageBlogList(1);
    async function getPageBlogList(page) {
      let { code, message, data } = await getPageBlog(page);
      if (code === 200 && message === "成功") {
        loading.value = false
        cuurentListBlog.value = data.result;
        allSize.value = data.pageSize;
        return;
      }
      ElMessage.error(message);
    }

    //计算分页
    function handleCurrentChange(val) {
      loading.value = true
      getPageBlogList(val);
    }
    return {
      loading,
      indexPage,
      cuurentListBlog,
      getDateYear,
      BASEURL,
      handleCurrentChange,
      allSize,
    };
  },
});
</script>
<style lang="less" scoped>
#main {
  position: relative;
  width: 100%;
  height: 100%;
  transition: all 4s ease 0s;
  .item-box {
    width: 90%;
    padding: 4%;
    .item {
      width: 100%;
      display: flex;
      justify-content: space-between;
      margin: 30px 0px;
      h2 {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      &:hover {
        cursor: pointer;
      }
      .item-left {
        display: flex;
        flex-flow: column;
        justify-content: space-between;
        .text {
          margin: 10px 0px;
          font-size: 16px;
          line-height: 1.625;
          color: #555;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .span {
          font-size: 12px;
          color: #888;
        }
      }
      .item-right {
        height: 150px;
        img {
          border-radius: 4px;
          box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
          width: 100%;
          height: 100%;
        }
      }
    }
  }
  .footer {
    width: 90%;
    // position: absolute;
    // bottom: 0px;
    .btn {
      width: 100%;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    }
  }
}

#main /deep/ .el-card {
  width: 100%;
  border: none;
}
#main /deep/ .el-card__body {
  background: rgb(250, 251, 255);
}
#no-blog ::v-deep .el-card {
  box-shadow: none;
  -webkit-box-shadow: none;
  color: #b0b0b9;
  font-size: 20px;
}
</style>